import request from '@/axios'
import { agentRequest } from '@/utils/cache'
import { treeDicItemModel } from '../types/dictType'

function formatRequestUri(name) {
  return 'hr/TreeDic/' + name
}

export function getTrees(dicId: string, pid: string): Promise<treeDicItemModel[]> {
  const key = 'DicTree_' + dicId + '_' + pid
  return agentRequest(key, () => {
    return request.get({
      url: formatRequestUri('GetDicTree'),
      params: {
        dicId,
        pid
      }
    })
  })
}
export function getTree(query) {
  return request.post({
    url: formatRequestUri('GetTree'),
    data: query
  })
}

export function DeleteItem(id) {
  return request.get({
    url: formatRequestUri('Delete'),
    params: {
      id
    }
  })
}

export function Add(data) {
  return request.post({
    url: formatRequestUri('Add'),
    data
  })
}

export function Set(id, data) {
  return request.post({
    url: formatRequestUri('Set'),
    data: {
      Id: id,
      Datum: data
    }
  })
}
export function Stop(id) {
  return request.get({
    url: formatRequestUri('Stop'),
    params: {
      id
    }
  })
}
export function Enable(id) {
  return request.get({
    url: formatRequestUri('Enable'),
    params: {
      id
    }
  })
}
export function Get(id) {
  return request.get({
    url: formatRequestUri('Get'),
    params: {
      id
    }
  })
}
export function Move(id, toId) {
  return request.post({
    url: formatRequestUri('Move'),
    data: {
      FromId: id,
      ToId: toId
    }
  })
}

export function GetNames(dicId, vals) {
  return request.post({
    url: formatRequestUri('GetNames'),
    data: {
      Id: dicId,
      Value: vals
    }
  })
}
